package com.ll19.test.db;

import static org.junit.Assert.assertNotNull;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.ll19.Constants;
import com.ll19.dao.RoleDao;
import com.ll19.dao.UserDao;
import com.ll19.model.Address;
import com.ll19.model.Role;
import com.ll19.model.User;

public class DBtest extends BastDBTestCase {

	// 注入EntityManager
	@PersistenceContext
	private EntityManager entityManager;

	// 根据类型注入
	@Autowired
	private UserDao dao;
	@Autowired
	private RoleDao rdao;

	@Test
	// 测试连级删除
	public void testMultiDel() throws Exception {

		// 测试dao
		User user = new User("testuser");
		user.setPassword("testpass");
		user.setFirstName("Test");
		user.setLastName("Last");
		Address address = new Address();
		address.setCity("Denver");
		address.setProvince("CO");
		address.setCountry("USA");
		address.setPostalCode("80210");
		user.setAddress(address);
		user.setEmail("testuser@appfuse.org");
		user.setWebsite("http://raibledesigns.com");

		Role role = rdao.getRoleByName(Constants.USER_ROLE);
		assertNotNull(role.getId());
		user.addRole(role);

		user = dao.save(user);

		System.out.println(user.getId());

		entityManager.flush();
		entityManager.clear();

		System.out.println("save");

		// 测试usermanager

	}

	// 测试回滚
	public void testRollBack() throws Exception {

		// 测试两个方法的回滚

	}

}
